package com.cicc.springboot.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicc.springboot.common.dto.OrderExcelDTO;
import com.cicc.springboot.common.dto.R;
import com.cicc.springboot.entity.PurchaseOrderEntity;
import com.cicc.springboot.entity.SupplierBasicEntity;
import com.cicc.springboot.mapper.PurchaseOrderMapper;
import com.cicc.springboot.mapper.SupplierBasicMapper;
import com.cicc.springboot.model.PurchaseOrderQuery;
import com.cicc.springboot.service.PurchaseOrderService;
import com.cicc.springboot.utile.OrderDetailListener;
import com.cicc.springboot.utile.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName PurchaseOrderServiceImpl
 * @Description TODO
 * @Author 转轮王
 * @Date 2024/9/30 16:04
 * @Version 1.0
 */


@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<SupplierBasicMapper, SupplierBasicEntity> implements PurchaseOrderService {


    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;


    //用于判断sql影响的行数
    private final int rows = 0;

    //创建雪花算法实例
    SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(1, 1);


    /**
     * 查询采购订单表信息
     */
    @Override
    public R<Page<PurchaseOrderEntity>> queryPurchaseOrderPage(int currentPage, int pageSize, String createTimeStart, String createTimeEnd, String supplierName, String supplierCode) {

        Page<PurchaseOrderEntity> page = new Page<>(currentPage, pageSize);

        QueryWrapper<PurchaseOrderEntity> queryWrapper = new QueryWrapper<>();

        // 时间区间查询
//        if (createTimeStart != null && !createTimeStart.isEmpty() &&
//                createTimeEnd != null && !createTimeEnd.isEmpty()) {
//            queryWrapper.between("create_time", createTimeStart, createTimeEnd);
//        }
        // 时间区间查询
        if (createTimeStart != null && !createTimeStart.isEmpty()) {
            queryWrapper.ge("create_time", createTimeStart); // 大于等于开始时间
        }

        if (createTimeEnd != null && !createTimeEnd.isEmpty()) {
            queryWrapper.le("create_time", createTimeEnd); // 小于等于结束时间
        }


        if (supplierName != null && !supplierName.isEmpty()) {
            queryWrapper.eq("supplier_name", supplierName);
        }
        if (supplierCode != null && !supplierCode.isEmpty()) {
            queryWrapper.eq("supplier_code", supplierCode);
        }

        Page<PurchaseOrderEntity> PurchaseOrderEntityPage = purchaseOrderMapper.selectPage(page, queryWrapper);

        return R.ok(PurchaseOrderEntityPage);

    }


    /**
     * 新增或修改采购订单表
     *
     * @param purchaseOrderQuery
     * @return
     */
    @Override
    public R<String> savePurchaseOrder(PurchaseOrderQuery purchaseOrderQuery) {
        if (purchaseOrderQuery.getId() != null) {
            int updateCount = purchaseOrderMapper.updateById(purchaseOrderQuery);
            if (updateCount > rows) {
                return R.ok(null, "修改成功");
            } else {
                return R.failed("更新失败");
            }
        } else {
            purchaseOrderQuery.setOrderCode(snowflakeIdWorker.nextId());
            System.out.println(purchaseOrderQuery + "123132123123123");
            int insertCount = purchaseOrderMapper.insert(purchaseOrderQuery);
            if (insertCount > rows) {
                return R.ok(null, "新增成功");
            } else {
                return R.failed("新增失败");
            }
        }
    }


    /**
     * 删除采购订单信息
     *
     * @param id
     * @return
     */
    @Override
    public R<String> deletePurchaseOrder(long id) {
        int deleteCount = purchaseOrderMapper.deleteById(id);
        if (deleteCount > rows) {
            return R.ok(null, "删除成功");
        } else {
            return R.failed("删除失败 未找到对应的订单");
        }
    }

    /**
     * 查询全部供应商的名称
     *
     * @return 返回封装在 R 对象中的供应商名称数组
     */
    @Override
    public R<String[]> queryAllSupplierName() {
        // 查询所有供应商的名称
        List<String> supplierNames = this.baseMapper.selectList(
                        new LambdaQueryWrapper<SupplierBasicEntity>().select(SupplierBasicEntity::getSupplierName)
                ).stream()
                .map(SupplierBasicEntity::getSupplierName)
                .toList();

        // 将结果转换为数组
        String[] namesArray = supplierNames.toArray(new String[0]);

        // 返回封装在 R 对象中的结果
        return R.ok(namesArray);
    }


    /**
     * 根据供应商名称查询供应商编码
     *
     * @param supplierName
     * @return
     */
    @Override
    public R<String> querySupplierCodeByName(String supplierName) {
        // 使用 MyBatis Plus 的 LambdaQueryWrapper 查询供应商编码
        String supplierCode = this.baseMapper.selectOne(
                new LambdaQueryWrapper<SupplierBasicEntity>()
                        .select(SupplierBasicEntity::getErpCode)
                        .eq(SupplierBasicEntity::getSupplierName, supplierName)  // 根据名称查找
        ).getErpCode(); // 获取编码

        // 检查是否找到供应商编码
        if (supplierCode != null) {
            return R.ok(supplierCode);
        } else {
            return R.failed("未找到供应商编码");
        }
    }


}
